package com.dog.view.model;

import com.dog.model.BaseEntity;
import com.dog.service.IBaseService;
import org.ajax4jsf.model.DataVisitor;
import org.ajax4jsf.model.ExtendedDataModel;
import org.ajax4jsf.model.Range;
import org.ajax4jsf.model.SequenceRange;

import javax.faces.context.FacesContext;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Kuttubek Aidaraliev
 * Date: 13.04.2010
 * Time: 13:34:36
 */
public class BaseExtendedDataModel extends ExtendedDataModel {

    public enum Order {
        ASCENDING, DESCENDING
    }

    //private static Session manager = DAO.getSession();
    private IBaseService manager;
    private Map<Long, BaseEntity> wrappedData = new HashMap<Long, BaseEntity>();

    /*public void save(ActionEvent event) {
        BaseEntity baseEntity = (BaseEntity) this.getRowData();
        manager.update(baseEntity);
    }

    public void delete(ActionEvent event) {
        BaseEntity baseEntity = (BaseEntity) this.getRowData();
        manager.delete(baseEntity);
    }*/


    @Override
    public void walk(FacesContext context, DataVisitor visitor, Range range,
                     Object argument) throws IOException {

        int firstRow = ((SequenceRange) range).getFirstRow();
        int numberOfRows = ((SequenceRange) range).getRows();

        //wrappedKeys = new ArrayList<Long>();
        List<BaseEntity> list = manager.getByRange(firstRow,
                numberOfRows, currentSortField, currentSortAscending);
        for (BaseEntity item : list) {
            //wrappedKeys.add(item.getId());
            getWrappedData().put(item.getId(), item);
            visitor.process(context, item.getId(), argument);
        }

    }

    @Override
    public int getRowCount() {
        return (int) manager.getTotalCount();
    }


    @Override
    public Object getRowData() {
        if (currentPk == null) {
            return null;
        } else {
            BaseEntity ret = getWrappedData().get(currentPk);
            if (ret == null) {
                ret = manager.get(currentPk);
                getWrappedData().put(currentPk, ret);
                return ret;
            } else {
                return ret;
            }
        }
    }

    @Override
    public Object getRowKey() {
        return currentPk;
    }

    @Override
    public void setRowKey(Object key) {
        /*
        * TODO check: Not sure if this will work (since the
        * SerializableDataModel uses int, not long)
        */
        this.currentPk = (Long) key;
    }

    @Override
    public boolean isRowAvailable() {
        if (currentPk == null) {
            return false;
        } else {
            return (manager.get(currentPk) != null);
        }
    }

    public Map<Long, BaseEntity> getWrappedData() {
        return this.wrappedData;
    }

    protected void setWrappedData(Map<Long, BaseEntity> wrappedData) {
        this.wrappedData = wrappedData;
    }

    public void setManager(IBaseService manager) {
        this.manager = manager;
    }

    @Override
    public int getRowIndex() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setRowIndex(int rowIndex) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void setWrappedData(Object data) {
        throw new UnsupportedOperationException();
    }

    private Long currentPk;
    private String currentSortField;
    private boolean currentSortAscending;
    //private List<Long> wrappedKeys = null;

    public void setSortBy(String sortField, boolean sortAscending) {
        this.currentSortField = sortField;
        this.currentSortAscending = sortAscending;
    }

}